﻿using System;
using System.Globalization;
using System.Threading;

namespace InfoJetSoft.Service.Util
{
	
	public class LocaleUtil
	{
		public static CultureInfo getLocale(TypeFormat typeFormat){
			if ( typeFormat.CurrencyLocale != null) 
			{
				try 
				{
					int lcid = System.Int32.Parse(typeFormat.CurrencyLocale);
					return new CultureInfo( lcid );
				}
				catch (Exception e) 
				{
					return Thread.CurrentThread.CurrentCulture;
				}
			}
			else if( typeFormat.Locale != null )
			{
				try 
				{
					int lcid = System.Int32.Parse(typeFormat.Locale);
					return new CultureInfo( lcid );
				}
				catch (Exception e) 
				{
					return Thread.CurrentThread.CurrentCulture;
				}
			}
			else 
			{
				return Thread.CurrentThread.CurrentCulture;
			}
		}

		public static void setCultureCurrencyLocaleSymbol(TypeFormat typeFormat, NumberFormat numberFormat)
		{
			if (typeFormat.CurrencyLocale != null) {
				try {
					int lcid = System.Int32.Parse(typeFormat.CurrencyLocale);
					CultureInfo cultureInfo = new CultureInfo( lcid );
					// cultureInfo.NumberFormat.CurrencySymbol;
					numberFormat.Culture = cultureInfo;
					numberFormat.CurrencySymbol = cultureInfo.NumberFormat.CurrencySymbol;
					numberFormat.CurrencyLocale = typeFormat.CurrencyLocale;
				}
				catch (Exception e) 
				{
					numberFormat.Culture = Thread.CurrentThread.CurrentCulture;
					numberFormat.CurrencySymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol;
					numberFormat.CurrencyLocale = Thread.CurrentThread.CurrentCulture.LCID + "";
				}
			}
			else if( typeFormat.Locale != null )
			{
				try 
				{
					int lcid = System.Int32.Parse(typeFormat.Locale);
					CultureInfo cultureInfo = new CultureInfo( lcid );

					numberFormat.Culture = cultureInfo;
					numberFormat.CurrencySymbol = cultureInfo.NumberFormat.CurrencySymbol;
					numberFormat.CurrencyLocale = typeFormat.Locale;
				}
				catch (Exception e) 
				{
					numberFormat.Culture = Thread.CurrentThread.CurrentCulture;
					numberFormat.CurrencySymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol;
					numberFormat.CurrencyLocale = Thread.CurrentThread.CurrentCulture.LCID + "";
				}
			}
			else 
			{
				numberFormat.Culture = Thread.CurrentThread.CurrentCulture;
				numberFormat.CurrencySymbol = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol;
				numberFormat.CurrencyLocale = Thread.CurrentThread.CurrentCulture.LCID + "";
			}
			//CID 0471 //2009.12.06 //Begin
			//总是使用当前Culture，当前Culture和Separator有关系，覆盖掉上面的设置。
			numberFormat.Culture = Thread.CurrentThread.CurrentCulture;
			//CID 0471 //2009.12.06 //End
		}

		public static string getCurrencySymbol(TypeFormat typeFormat)
		{
			if (typeFormat.CurrencyLocale != null) 
			{
				try 
				{
					int lcid = System.Int32.Parse(typeFormat.CurrencyLocale);
					CultureInfo cultureInfo = new CultureInfo( lcid );
					return cultureInfo.NumberFormat.CurrencySymbol;
				}
				catch (Exception e) 
				{
					return Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol;
				}
			}
			else 
			{
				return Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencySymbol;
			}
		}

	}
}